﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PM.Controls;
using PM.Controls.Common;
using System.Windows.Media.Animation;
using HealthManagement.Data.Service;
using HealthManagement.Data.Model;
using Telerik.Windows.Controls;

namespace HealthManagement
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : PMWindow
    {
        public LoginWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(LoginWindow_Loaded);
            this.KeyDown += new KeyEventHandler(LoginWindow_KeyDown);


            this.UserIdBox.Focus();

        }

       
        void LoginWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Storyboard sb = this.Resources["sbProliferation"] as Storyboard;
            sb.Begin();
        }

        void LoginWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                RadButton_Click(this, null);
                return;
            }


            if (e.Key == Key.Escape)
            {
                CloseLoginWindow();
                return;
            }
        }

        private void RadButton_Click(object sender, RoutedEventArgs e)
        {
            UserService ser = new UserService();

            if (UserIdBox.Value == null || string.IsNullOrEmpty(PasswordBox.Password))
            {
                Tools.ShowMessage(false, "医师编号或密码为空，请重新输入！");
                return;
            }
            string userid = UserIdBox.Value.ToString().Trim();
            string pwd = PasswordBox.Password.Trim();
            User user = ser.Login(userid, pwd);

            if (user != null)
            {
                if (user is DoctorUser)
                {
                    MainWindow window = new MainWindow(CloseLoginWindow, user);
                    window.Show();
                    Tools.PerformInEffect(window).Begin();
                }
                else if (user is MeasureUser)
                {
                    Tools.ShowMessage(false, "此用户没有登录权限！");
                }
            }
            else
            {
                Tools.ShowMessage(false, "医师编号或密码错误！");
            }
        }
        public void CloseLoginWindow()
        {
            this.CloseWindow();
        }


        private void cancel_click(object sender, RoutedEventArgs e)
        {
            UserIdBox.Value = null;
            PasswordBox.Password = string.Empty;
        }
    }
}
